package com.clothing.customization.controller;

import com.clothing.customization.dto.ApiResult;
import com.clothing.customization.service.AdminDashboardService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 管理员仪表盘控制器
 */
@RestController
@RequestMapping("/api/admin/dashboard")
@RequiredArgsConstructor
@PreAuthorize("hasRole('ADMIN')")
public class AdminDashboardController {
    
    private final AdminDashboardService dashboardService;
    
    /**
     * 获取仪表盘统计数据
     * @return 统计数据
     */
    @GetMapping("/stats")
    public ResponseEntity<ApiResult<Map<String, Object>>> getStats() {
        Map<String, Object> stats = dashboardService.getStats();
        return ResponseEntity.ok(ApiResult.success(stats));
    }
    
    /**
     * 获取最近订单
     * @return 最近订单列表
     */
    @GetMapping("/recent-orders")
    public ResponseEntity<ApiResult<List<Map<String, Object>>>> getRecentOrders() {
        List<Map<String, Object>> orders = dashboardService.getRecentOrders();
        return ResponseEntity.ok(ApiResult.success(orders));
    }
    
    /**
     * 获取最新用户
     * @return 最新用户列表
     */
    @GetMapping("/recent-users")
    public ResponseEntity<ApiResult<List<Map<String, Object>>>> getRecentUsers() {
        List<Map<String, Object>> users = dashboardService.getRecentUsers();
        return ResponseEntity.ok(ApiResult.success(users));
    }
    
    /**
     * 获取系统日志
     * @param type 日志类型
     * @return 系统日志列表
     */
    @GetMapping("/system-logs")
    public ResponseEntity<ApiResult<List<Map<String, Object>>>> getSystemLogs(@RequestParam(defaultValue = "all") String type) {
        List<Map<String, Object>> logs = dashboardService.getSystemLogs(type);
        return ResponseEntity.ok(ApiResult.success(logs));
    }
    
    /**
     * 获取订单图表数据
     * @param type 时间类型：week, month, year
     * @return 订单图表数据
     */
    @GetMapping("/order-chart")
    public ResponseEntity<ApiResult<Map<String, Object>>> getOrderChartData(@RequestParam(defaultValue = "month") String type) {
        Map<String, Object> chartData = dashboardService.getOrderChartData(type);
        return ResponseEntity.ok(ApiResult.success(chartData));
    }
    
    /**
     * 获取用户图表数据
     * @param type 类型：role, growth
     * @return 用户图表数据
     */
    @GetMapping("/user-chart")
    public ResponseEntity<ApiResult<Map<String, Object>>> getUserChartData(@RequestParam(defaultValue = "role") String type) {
        Map<String, Object> chartData = dashboardService.getUserChartData(type);
        return ResponseEntity.ok(ApiResult.success(chartData));
    }
} 